package com.zysc.processflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zysc.processflow.common.util.SendSMSUtils;
import com.zysc.processflow.common.util.StringUtils;
import com.zysc.processflow.entity.*;
import com.zysc.processflow.mapper.HandlerecorddetailMapper;
import com.zysc.processflow.service.*;
import com.zysc.processflow.common.base.BaseService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>
 * 处置流程记录详情表 服务实现类
 * </p>
 *
 * @author 正元智慧城市_
 * @since 2022-11-18
 */
@Service
public class HandlerecorddetailServiceImpl extends BaseService<HandlerecorddetailMapper, Handlerecorddetail> implements IHandlerecorddetailService {

    private ExecutorService executorService = Executors.newFixedThreadPool(50);

    @Autowired
    SendSMSUtils smsUtils;
    @Autowired
    IHandleprocessnodeService handleprocessnodeService;
    @Autowired
    IHandletaskService handletaskService;
    @Autowired
    IPersonService personService;
    @Autowired
    IPersonroleService personroleService;
    @Autowired
    IOrganizationService organizationService;
    @Autowired
    IPersonorgService personorgService;
    @Autowired
    IProcessOperationService processOperationService;
    @Autowired
    private IAttachmentService fileService;
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(HandlerecorddetailServiceImpl.class);
    @Override
    public List<Handlerecorddetail> GetHandlerecorddetailListByPage(IPage<Handlerecorddetail> page, String disposername) {
        LambdaQueryWrapper<Handlerecorddetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(!StringUtils.isNull(disposername), Handlerecorddetail::getSuggestion,disposername);
        List<Handlerecorddetail> list = new ArrayList<>();
        if (page != null) {
            list = this.baseMapper.selectPage(page, queryWrapper).getRecords();
        }else{
            list = this.baseMapper.selectList(queryWrapper);
        }
        return list;
    }

    @Override
    public List<Handlerecorddetail> GetApprovalHistoryByRecordId(String recordid) {
        List<Handlerecorddetail> list = new ArrayList<>();
        LambdaQueryWrapper<Handlerecorddetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Handlerecorddetail::getRecordid,recordid)
                .orderByAsc(Handlerecorddetail::getHandletime);
        list = this.list(queryWrapper);
        list.stream().forEach(item->{
            List<Attachment> files = fileService.getFilesByLinkId(item.id);
            item.setAttachments(files);
        });
        return list;
    }

    @Override
    public int GetApprovalHistoryCountByRecordId(String recordid) {
        List<Handlerecorddetail> list = new ArrayList<>();
        LambdaQueryWrapper<Handlerecorddetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Handlerecorddetail::getRecordid,recordid)
                .orderByAsc(Handlerecorddetail::getHandletime);
        list = this.list(queryWrapper);
        return list.size();
    }
    @Override
    public Boolean addSystemRecordDetail(String recordId, String detailType, Long time) {
        //1.先查找有没有,有的话,就不加了
        LambdaQueryWrapper<Handlerecorddetail> qw = new LambdaQueryWrapper<>();
        qw.eq(Handlerecorddetail::getRecordid,recordId)
                .eq(Handlerecorddetail::getResult,detailType);
        List<Handlerecorddetail> list = this.list(qw);
        if(list.size()>0){
            return true;
        }
        //2.加
        Handlerecorddetail detail = new Handlerecorddetail();
        detail.setDisposerid("system");
        detail.setType("系统任务");
        detail.setRecordid(recordId);
        detail.setResult(detailType);
        detail.setHandletime(time);
        return this.save(detail);
    }
}
